package cosmos.dubbo.service.impl;

import cosmos.dubbo.DataStore;
import cosmos.dubbo.domain.User;
import cosmos.dubbo.dto.UserDto;
import cosmos.dubbo.enums.Status;
import cosmos.dubbo.service.UserService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * .
 *
 * @author sunhao(sunhao.java@gmail.com)
 * @version V1.0, 2016-2-4 10:30
 */
@Service
public class UserServiceImpl implements UserService {

    @Override
    public Long save(UserDto userDto) {
        User user = new User();
        user.setId(DataStore.nextId());
        user.setCreateAt(new Date());
        user.setSex(userDto.getSex());
        user.setStatus(Status.AUDITING);
        user.setTelNo(user.getTelNo());
        user.setUserName(userDto.getUserName());

        return DataStore.save(user);
    }

    @Override
    public Long delete(Long id) {
        return DataStore.delete(id);
    }

    @Override
    public Long update(UserDto userDto) {
        User user = new User();
        user.setId(userDto.getId());
        user.setSex(userDto.getSex());
        user.setTelNo(user.getTelNo());
        user.setUserName(userDto.getUserName());

        return DataStore.update(user);
    }

    @Override
    public User loadUser(Long id) {
        return DataStore.get(id);
    }

    @Override
    public Page<User> list(Pageable pageable) {
        return DataStore.list(pageable);
    }
}
